<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\ShopEmployee;
use App\Repository\Models\User;
use App\Repository\Models\ShopRightAuth;
use App\Repository\Models\ShopRightMenu;
use App\Repository\Models\ShopRightOperation;
use App\Repository\Repositories\Interfaces\ShopEmployeeRepository;
use App\Repository\Repositories\Interfaces\ShopRightAuthRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 订单管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ShopEmployeeController extends BaseController
{
    /**
     * @var ShopRepository
     */
    protected $shop;

    protected $sp_uid;
    protected $user_id;

    public function __construct(ShopEmployeeRepository $shop,Request $request,ShopRightAuthRepository $shopRightAuth)
    {
        parent::__construct();

        $this->shop = $shop;
        $this->shopRightAuth = $shopRightAuth;
        $this->sp_uid = array_get($this->user, 'id');
        $this->shop_id= array_get($this->user, 'shop_id');

        $this->request = $request;
        //用户只能操作自身所属数据
        $this->shop->scopeQuery(function ($query) {
            return $query->where('shop_id',$this->shop_id);

        });
    }

    public function index()
    {
        $shops = $this->shop->with(['shopRightAuth'])->paginate($this->perPage);

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shops, true);
    }

    public function show($id)
    {
        $shop = $this->shop->with(['shopRightAuth'])->find($id);
        $shop['data']['auth']=[];
        if(!empty($shop['data']['shop_right_auth'])){
            $shop_right_auth=$shop['data']['shop_right_auth'];
            $auth=[];
            foreach($shop_right_auth as $k=>$v){
                if($v['resource_type']==1){
                    $auth[$v['resource_id']]=ShopRightMenu::query()->find($v['resource_id']);
                }else{
                    $operation=ShopRightOperation::query()->find($v['resource_id']);
                    $auth[$operation['menu_id']]['data']=$operation;
                }
            }
            $shop['data']['auth']=$auth;
        }
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shop['data']);
    }


    public function update(Request $request,$id)
    {

        $shop=$request->all();

        \DB::beginTransaction();
        try{
            ShopRightAuth::query()->where('emp_id',$id)->delete();
            //添加菜单权限
            if(!empty($shop['menu'])){
                $menu_pid=[];
                $add = [];
                foreach($shop['menu'] as $k =>$v){
                    $menu=ShopRightMenu::query()->where('id',$v)->get()->toArray();
                    if(!in_array($menu[0]['pid'],$menu_pid)){
                        $menu_pid[]=$menu[0]['pid'];
                    }
                    $add=[
                        'emp_id'=>$id,
                        'resource_id'=>$v,
                        'resource_type'=>1,
                    ];
                    ShopRightAuth::query()->create($add);
                }
                foreach($menu_pid as $k=>$v){
                    $add=[
                       'emp_id'=>$id,
                       'resource_id'=>$v,
                       'resource_type'=>1,
                    ];
                    ShopRightAuth::query()->create($add);
                }
            }
            //添加操作权限
            if(!empty($shop['operation'])){
                foreach($shop['operation'] as $k=>$v){
                    $add=[
                       'emp_id'=>$id,
                       'resource_id'=>$v,
                       'resource_type'=>2,
                    ];
                    ShopRightAuth::query()->create($add);
                }
            }
            // 更新 员工主表
            if(!empty($shop['employee'])) {
                $params = $shop['employee'];
                $this->shop->update( $params,$id);
            }
            \DB::commit();
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $add);
        }catch (\Exception $e) {
            \DB::rollback(); //事务回滚;
            return $this->error($e->getMessage());
        }

    }
    public function store(){
        $data= $this->request->all();
        // 获取该手机号的 用户信息 // 昵称 user_id
        $user = User::query()->where('phone',$data['phone'])->first()->toArray();
        $params = [];
        $params['user_phone']   = $data['phone'];
        $params['user_captcha'] = $data['code'];
        $params['user_nick']    = $user['name'];
        $params['user_id']      = $user['id'];
        $params['shop_id']      = $this->shop_id;
        $ret=$this->shop->create($params);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $ret['data']);
    }

    public function nav($id){

        $ret=$this->shop->getSellerNav($this->user_id);

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $ret);

    }

    public function delete($id){
        $ret=$this->shop->delete($id);
        // 同时删除 shopRightAuth
        ShopRightAuth::query()->where('emp_id',$id)->delete();

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $ret);
    }
    public function checkEmp($phone){
        $ret=$this->shop->findWhere(['user_phone'=>$phone]);
        if (count($ret['data'])>0){
            return $this->error(ErrorConst::FAILED_CODE_MSG);
        }else{
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $ret);
        }
    }





}
